package com.wenshi.app.activity;


import android.util.Log;
import android.widget.ListView;


import com.wenshi.app.activity.R;
import com.wenshi.app.view.WsLoadMoreListView;
import com.wenshi.app.view.WsSwipeRefreshAndLoadMoreLayout;
import com.wenshi.view.IKeyValue;


import java.util.ArrayList;




/**
 * 该类适用于带listview下拉刷新的布局使用  直接extends该类即可
 */
public abstract class WsReFreshAndLoadMoreActivity extends WsListViewActivity implements WsSwipeRefreshAndLoadMoreLayout.IRefreshAndLoadMoreLisenter {


    private WsLoadMoreListView mLoadMoreListView;
    private WsSwipeRefreshAndLoadMoreLayout mRefreshAndLoadMoreView;
    private WsLoadMoreListView.OnListViewScrollListener onListViewScrollListener;
    private boolean renderView_ok = false;
    private boolean hassetadapter = false;

    public void setOnListViewScrollListener(WsLoadMoreListView.OnListViewScrollListener onListViewScrollListener) {
        this.onListViewScrollListener = onListViewScrollListener;
    }

    protected void initLayOutAndListView() {
        if (!renderView_ok) {

            getWsWiewDelegate().setContentView(R.layout.wsview_activity_load_and_refresh);
            mLoadMoreListView = (WsLoadMoreListView) findViewById(R.id.load_more_list);
            //   mLoadMoreListView = (WsLoadMoreListView) findViewById(WsViewTools.getResource(this,"load_more_list","id"));
            mRefreshAndLoadMoreView = (WsSwipeRefreshAndLoadMoreLayout) findViewById(R.id.refresh_and_load_more);
            //  mRefreshAndLoadMoreView = (WsSwipeRefreshAndLoadMoreLayout) findViewById(WsViewTools.getResource(this,"refresh_and_load_more","id"));
            mLoadMoreListView.setWsSwipeRefreshAndLoadMoreLayout(mRefreshAndLoadMoreView);
            mRefreshAndLoadMoreView.setWsLoadMoreListView(mLoadMoreListView);
            renderView_ok = true;
        }
    }

    public ListView getListView() {
        return mLoadMoreListView;
    }

    protected void addAdapterData(ArrayList<IKeyValue> items) {
        adapter.addItemMore(items);
        whenloaddata(items, false);
    }

    protected void whenloaddata(ArrayList<IKeyValue> items, boolean isinit) {
        super.whenloaddata(items, isinit);

        if (items.size() == 0) {
            mLoadMoreListView.setHaveMoreData(false, isinit);
        } else {
            mLoadMoreListView.setHaveMoreData(true, isinit);
        }
        mRefreshAndLoadMoreView.setRefreshing(false);
        mLoadMoreListView.onLoadComplete();
    }


}
